
local ffi = require('ffi');
local View = require("view.render");
local SDL2 = ffi.load('SDL2');  

--调整屏幕显示位置
--窗口,修改位置的视图,高宽比
function SetShowConfig(window,views)
    local textListView = View.TextListView.new(window);
    local screen = ffi.new('SDL_Rect');
    local save = false;
    local mainView = nil;
    textListView.x = 100;
    textListView.y = 20;
    textListView.w = 500;
    textListView.h = 1500;
    textListView.textFont = {
        fontColor = 0xffffffff,
    }
    

    textListView.texts = {
        {text = '放大',Click = function() local scale = screen.h/screen.w; screen.w = screen.w + 5; screen.h = screen.w*scale; end},
        {text = '缩小',Click = function() local scale = screen.h/screen.w; screen.w = screen.w - 5; screen.h = screen.w*scale; end},
        {text = '返回',Click = function() mainView = nil; end,},
        {text = '保存设置',Click = function() save = true; end},
        {text = '退出设置',Click = function() window.quit = true; end};
    };
    --备份window状态
    local win_event = window:GetEventCallBack();

    local function Event(event)
        if(mainView)then
            mainView:Event(event);
            return ;
        end
            --鼠标时间
        event = event.motion;
        local x,y = event.x,event.y;

        local self = {drawPos={x=0,y=0}};
        if(selectView)then
            self.drawPos = selectView:GetRenderRect();
        end

        --触摸事件
        function self:OnTouch(event)
            
            --查找匹配的views
            local x,y = event.x,event.y;
            local index = 0;
            for k=#views,1,-1 do
                local v = views[k];
                local rect = v:GetRenderRect();
                local dx,dy = rect.x,rect.y;
                dx = x - dx;
                dy = y - dy;
                if(dx>0 and dy>0 and dx<rect.w and dy<rect.h)then 
                    --选择了当前的视图
                    index = k;
                    selectView = v;
                    screen = rect;
                    break;
                end
            end

            --重新排序
            if(index>0)then
                table.remove(views,index);
                table.insert(views,selectView);
            end
            
        end
    
        if(event.type==0x400)then
            --0x400是移动事件
            if(event.state==1)then
                --左键按压
                self.drawPos.y = self.drawPos.y + event.yrel;
                self.drawPos.x = self.drawPos.x + event.xrel;
                self.selectY = event.y;
                self.selectX = event.x;
                self.drag = true;
            end
        elseif(event.type==0x401)then
            --按下事件
            self.selectY = event.y;
            self.selectY = event.x;
            self.drag = nil;
            self:OnTouch(event);
        elseif(event.type==0x402 and self.selectY)then
            --按键抬起
        end
    end

    --设置消息事件
    window:SetEventCallBack(Event);

    --进入消息循环
    window:GameLoop(function()
        window:RenderClear();

        if(window:KeyState('Escape') or window:KeyState('AC Back'))then
            mainView = textListView;
        end

        for k=1,#views do
            local v = views[k];
            v:Render();
            local rect = v:GetRenderRect();
            if(selectView==v)then
                window:DrawRect(rect.x,rect.y,rect.w,rect.h,0xff0000ff);  --红色方框
            else
                window:DrawRect(rect.x,rect.y,rect.w,rect.h,0xffffffff);  --白色方框
            end
        end

        if(mainView)then
            mainView:Render();
        end

        return false;

        --window.quit = true;
    end);

    for k,v in ipairs(views)do
        if(v.SetRenderRect)then
            local rect = v:GetRenderRect();
            v:SetRenderRect(rect.x,rect.y,rect.w,rect.h);
        end
    end

    window:SetEventCallBack(win_event);
    window.quit = nil;
end

